package com.kxq.zhuoyue.controller;


import com.kxq.zhuoyue.po.MaterialGroup;
import com.kxq.zhuoyue.service.MaterialGroupService;
import com.kxq.zhuoyue.util.UUID;
import com.kxq.zhuoyue.util.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xjh
 * @since 2023-09-04
 */
@RestController
@RequestMapping("/materialGroup")
public class MaterialGroupController {

    @Autowired
    private MaterialGroupService materialGroupService;

    /**
     * 查询所有分组
     * @return
     */
    @GetMapping("/selectAll")
    public AjaxResult selectAll(){
        List<MaterialGroup> list = materialGroupService.list();
        return AjaxResult.success("查询成功!",list);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody MaterialGroup materialGroup){
        String id = UUID.getOrderIdByTime();
        materialGroup.setMaterialGroupId(id);
        boolean save = materialGroupService.save(materialGroup);
        if (save){
            return AjaxResult.success("添加分组成功 ！");
        }
        else {
            return AjaxResult.success("添加分组失败 !");
        }

    }

    @PostMapping("/delete/{materialGroupId}")
    public AjaxResult delete(@PathVariable String materialGroupId){
        boolean b = materialGroupService.removeById(materialGroupId);
        if (b){
            return AjaxResult.success("删除分组成功 ！");
        }
        else {
            return AjaxResult.success("删除分组失败 !");
        }
    }
}

